【1】params

params: 指定request中必须包含某些参数值是,才让该方法处理。

@RequestMapping(value = "testParamsAndHeaders", params = { "username","age!=10" })
public String testParamsAndHeaders() {
    System.out.println("testParamsAndHeaders");
    return SUCCESS;
}

params 只是判断url 或者 form data 中的参数是否复合params的定义,并不会直接绑定数据到方法的参数中!

2 @PathVariabl

绑定路径中的占位符参数到方法参数变量中;只能绑定路径中的占位符参数,且路径中必须有参数。无论是 GET 或者POST 只要 URL中有参数即可!实例如下:GET
Request URL:http://localhost:8080/SpringMVC-1/springmvc/testPathVariable/1

1
2
3
4
5
6
<form action="springmvc/testPathVariable/1" method="POST">
<input type="text" name="username" value=""/>
<input type="text" name="age" value=""/>
<input type="text" name="sex" value=""/>
<input type="submit" value="submit"/>
</form>

【注意】如果URL中无参数,将会出错;如果URL有参数,但是没有使用@PathVariabl该注解,那么URL的参数不会默认与方法参数绑定!方法里的参数会默认绑定表单里面对应的参数!

后台code

如果参数名与占位符一致,则可直接使用@PathVariable;如果不一致,则在@PathVariable( )括号内绑定占位符。

@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id2) {
    System.out.println("testPathVariable: " + id2);
    return SUCCESS;
}

【3】@RequestParam
该注解相关属性如下:

value:参数key,可以不写,默认为””;

name:和value作用一样;

required:默认值为true,可以不写;

获取URL或者 form data 中的参数

前台请求实例如下:

GET

1 POST
<form action="springmvc/testRequestParam" method="POST">
    <input type="text" name="userName" value=""/>
    <input type="text" name="age" value=""/>
    <input type="text" name="sex" value=""/>
    <input type="submit" value="submit"/>
</form>

注意 :

GET中的参数形式为:username=tom&age=11&sex=boy

POST中的参数形式为:以键值对形式保存在form data

后台代码示例:

@RequestMapping(value="/regist",produces="application/json;charset=utf-8")
@ResponseBody
public String regist(SysUser sysUser , @RequestParam(required=true,name="sex") String sex){
    String userName = sysUser.getUserName();
    String age = sysUser.getAge();
    //...
    return "regist success";
}

总得来说,均是键值对形式,与@PathVariabl中的占位符形式不同!!!

#@RequestParam注解

@RequestParam注解比较简单,它用于将请求参数区数据映射到功能处理方法的参数上,自SpringMVC4.2之后,@RequestParam注解内部有4个参数:

  • String name
  • String value
  • boolean required
  • String defaultValue

1.name与value属性,其中name和value分别是URL参数的名称,即二者没区别,我个人比较喜欢用name,因为在HTML的表单中我们都是用name属性来设置URL参数名称的,所以在注解上也使用name这个属性的话,会更直观一些。示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package org.zero01.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/test")
public class Test {

@RequestMapping("test.do")
// 指定将username参数的值传递到该方法的name参数上
public void test(@RequestParam(name = "username") String name) {
System.out.println(name);
}

@RequestMapping("user.do")
// 指定将username参数的值传递到该方法的user参数上,alias参数的值则传递到该方法的a参数上
public void userAndAlias(@RequestParam(name = "username")String user, @RequestParam(name = "alias")String a) {
System.out.println(user);
System.out.println(a);
}
}

2.required属性,该属性用于指定某个参数是否是必须的,默认值为true,表示请求中一定要有相应的参数,否则将报404错误码,示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package org.zero01.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/test")
public class Test {

@RequestMapping("test.do")
// 指定username参数是必须的,如果url上没有的话就会报错
public void test(@RequestParam(name = "username", required = true) String name) {
System.out.println(name);
}

@RequestMapping("user.do")
// 指定username与alias参数不是必须的,即便url上没有也不会报错
public void userAndAlias(@RequestParam(name = "username", required = false) String user, @RequestParam(name = "alias", required = false) String a) {
System.out.println(user);
System.out.println(a);
}
}

3.defaultValue属性,该属性用于指定参数的默认值,表示如果请求中没有同名参数时的默认值,默认值可以是SpEL表达式,如“#{systemProperties[‘java.vm.version’]}”。示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package org.zero01.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/test")
public class Test {

@RequestMapping("test.do")
// url上没有username参数时,给它设置一个默认值为nothing
public void test(@RequestParam(name = "username", defaultValue = "nothing") String name) {
System.out.println(name);
}

@RequestMapping("user.do")
// url上没有username以及alias参数时,给它设置一个默认值为nothing
public void userAndAlias(@RequestParam(name = "username", defaultValue = "nothing") String user, @RequestParam(name = "alias", defaultValue = "nothing") String a) {
System.out.println(user);
System.out.println(a);
}
}